/*
 * SVGGenerator.cpp
 *
 *  Created on: 20.11.2012
 *      Author: Lukas Gemela
 */

#include "SVGGenerator.h"

SVGGenerator::SVGGenerator() {
}

SVGGenerator::~SVGGenerator() {
}

void SVGGenerator::findLimitsForY(double value, LIMITS& limits) {
	if (value < limits.ymin)
		limits.ymin = value;
	else if (value > limits.ymax)
		limits.ymax = value;
}

void SVGGenerator::findLimitsForX(double value, LIMITS& limits) {
	if (value < limits.xmin)
		limits.xmin = value;
	else if (value > limits.xmax)
		limits.xmax = value;
}

/**
 * hledá celkovou relativní velikost grafu tak, aby všechny body byly vykresleny
 */
LIMITS SVGGenerator::findLimits(LPAPPROXPOINT ignewPoints,
		LPAPPROXPOINT bgPoints, LPAPPROXPOINT igPoints, int len) {
	LIMITS limits = { 0, 0, 0, 0 };
	for (int i = 0; i < len; i++) {
		findLimitsForY(ignewPoints[i].y, limits);
		findLimitsForX(TimeUtils::convertWinToSec(ignewPoints[i].x), limits);
	}

	for (int i = 0; i < len; i++) {
		findLimitsForY(bgPoints[i].y, limits);
		findLimitsForX(TimeUtils::convertWinToSec(bgPoints[i].x), limits);
	}

	for (int i = 0; i < len; i++) {
		findLimitsForY(igPoints[i].y, limits);
		findLimitsForX(TimeUtils::convertWinToSec(igPoints[i].x), limits);
	}

	return limits;

}

/**
 * Vykreslí graf pro funkce ignewPoints, bgPoints, igPoints do souboru output
 */
void SVGGenerator::draw(LPAPPROXPOINT ignewPoints, LPAPPROXPOINT bgPoints,
		LPAPPROXPOINT igPoints, int len, string output) {
	ofstream svgFile(output.c_str());
	if (!svgFile) {
		cout << "Cannot open SVG file.\n";
		return;
	}
	double left = 0;
	double right = 0;
	double top = 0;
	double bottom = 0;

	LIMITS limits = findLimits(ignewPoints, bgPoints, igPoints, len);

	//celkové hranice svg grafu
	left = limits.xmin - 0.1 * (limits.xmax - limits.xmin);
	right = limits.xmax + 0.1 * (limits.xmax - limits.xmin);
	bottom = limits.ymin - 0.1 * (limits.ymax - limits.ymin);
	top = limits.ymax + 0.1 * (limits.ymax - limits.ymin);

	PlotterParams params;
	SVGPlotter plotter(cin, svgFile, cerr, params);
	if (plotter.openpl() < 0) // open Plotter
			{
		cerr << "Couldn't open Plotter!\n";
		return;
	}

	plotter.fspace(left, bottom, right, top); // specify user coor system
	plotter.flinewidth((top - bottom) / 1000); // line thickness in user coordinates
	plotter.erase(); // erase Plotter's graphics display

	plotter.pencolorname("black"); // path will be drawn in red
	plotter.move(0.0, 0.0);
	plotter.line(left, 0.0, right, 0.0);

	plotter.endpath();
	plotter.move(0.0, 0.0);
	plotter.flinewidth((right - left) / 1000);
	plotter.line(0.0, bottom, 0.0, top);

	plotter.endpath();

	plotter.flinewidth((top - bottom) / 1000);
	plotter.pencolorname("blue");

	drawFunction(bgPoints, len, plotter);

	plotter.pencolorname("red");

	drawFunction(igPoints, len, plotter);
	plotter.pencolorname("green");

	drawFunction(ignewPoints, len, plotter);

	plotter.closepl();
	svgFile.close();

}

/**
 * vykreslí jednu křivku na základě bodů func
 */
void SVGGenerator::drawFunction(LPAPPROXPOINT func, int len,
		SVGPlotter& plotter) {
	int x = 0;
	double y = func[0].y;
	plotter.move(x, y);

	for (int i = 1; i < len; i++) {
		int time = TimeUtils::convertWinToSec(func[i].x);

		plotter.fcontrel(time - x, func[i].y - y);

		x = time;
		y = func[i].y;
	}
	plotter.endpath();

}
